﻿Public Class FormMain
    Dim objWorkshopCurrent As New clsWorkshopCurrent
    Dim objUser As New clsUser
    Dim objPgdMes As New clsPgdMes
    Dim objProdSpec As New clsProdSpec
    Dim objTpMst As New clsTpMst
    Dim objTpDetailsMst As New clsTpDetailsMst
    Dim objFlbInfo As New clsFLBinfo
    Dim objAutoNumber As New clsAutoNumber
    Dim objProdInfo As New clsProdInfo
    Dim flb_info As DataTable
    Dim rb_checked_text As String = "3,2"
    Dim rb_checked_details_text As String = "6"
    Dim current_section As String '当前工序
    Dim s_id As String
    Dim dlno As String
    Private Sub FormMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.WindowState = FormWindowState.Maximized
        connectDB()
        current_section = GetIniFile("SECTION", "current_section", "", g_szInitFileName)
        RadioButton4.Checked = True
        txt_tray_no.Focus()
        initForm()
        setDgvData()
    End Sub

    Private Sub connectDB()
        g_szInitFileName = My.Application.Info.DirectoryPath & "\Client_YZXZ.ini"

        g_Server.szDrive = GetIniFile("SERVER", "DRIVE", "PostgreSQL ANSI", g_szInitFileName)
        g_Server.szIPadr = GetIniFile("SERVER", "IP", "", g_szInitFileName)
        g_Server.szDbn = GetIniFile("SERVER", "DBN", "ZHENANSF", g_szInitFileName)
        g_Server.szUid = GetIniFile("SERVER", "UID", "sa", g_szInitFileName)
        g_Server.szPas = GetIniFile("SERVER", "PAS", "sa", g_szInitFileName)
        g_Server.iOver = CInt(GetIniFile("SERVER", "OVER", "-1", g_szInitFileName))

        If False = g_fDBconnect And 0 = g_iDBconnResult Then
            If "" = g_Server.szIPadr Or "0.0.0.0" = g_Server.szIPadr Then
            Else
                g_fDBconnect = True

                'データベース接続
                With g_Server
                    g_iDBconnResult = DBacs.Connect(.szDrive, .szIPadr, .szDbn, .szUid, .szPas, .iOver)
                    If 1 <> g_iDBconnResult Then
                        .szDrive = CStr(IIf("PostgreSQL" = .szDrive, "PostgreSQL ANSI", "PostgreSQL"))
                        g_iDBconnResult = DBacs.Connect(.szDrive, .szIPadr, .szDbn, .szUid, .szPas, .iOver)
                    End If
                End With
            End If
        End If
    End Sub
    Private Sub getUserListByCJ()
        Dim dtUser As DataTable = objUser.getUserListByCJ(rb_checked_text, rb_checked_details_text)
        cmbOpers.Properties.Items.Clear()
        For index = 0 To dtUser.Rows.Count - 1
            cmbOpers.Properties.Items.Add(dtUser.Rows(index)("user_name"))
        Next
        cmbOpers.Properties.Items(0).CheckState = CheckState.Checked
        cmbOpers.Properties.DropDownRows = cmbOpers.Properties.Items.Count + 1
        cmbOpers.Properties.ShowButtons = False
        cmbOpers.Properties.ShowPopupCloseButton = False
    End Sub

    Private Sub getRbChechedText()
        If RadioButton4.Checked Then
            rb_checked_text = "3,2"
            rb_checked_details_text = "6"
        End If
        If RadioButton5.Checked Then
            rb_checked_text = "3,2"
            rb_checked_details_text = "8"
        End If
        If RadioButton6.Checked Then
            rb_checked_text = "3,2"
            rb_checked_details_text = "10"
        End If
        If RadioButton1.Checked Then
            rb_checked_text = "2"
            rb_checked_details_text = "1"
        End If
    End Sub

    Private Sub RadioButtonText_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton4.CheckedChanged, RadioButton5.CheckedChanged, RadioButton6.CheckedChanged, RadioButton1.CheckedChanged
        getRbChechedText()
        getUserListByCJ()
    End Sub

    Public Sub initForm()
        Dim dt As DataTable = objWorkshopCurrent.GetListByWorkShopIdAndCurrentSection("0", current_section)
        cmb_support_model.DisplayMember = "support_model"
        cmb_support_model.ValueMember = "b_pgd_sys_id"
        cmb_support_model.DataSource = dt
        If dt.Rows.Count > 0 Then
            If String.IsNullOrEmpty(cmb_support_model.Text) = False Then
                cmb_support_model.SelectedIndex = 0
                Dim dtPgdMes As DataTable = objPgdMes.getList(cmb_support_model.SelectedValue.ToString())
                txt_mes_project_code.Text = dtPgdMes.Rows(0)("mes_project_code").ToString()
                txt_mes_project_name.Text = dtPgdMes.Rows(0)("mes_project_name").ToString()
                lblProdSpecName.Text = objProdSpec.getSpecNameByID(cmb_support_model.Text)
                Dim dtt As DataTable = objProdSpec.selectAllbySpecId(cmb_support_model.Text)
                If dtt.Rows.Count > 0 Then
                    txtFlbType.Text = dtt.Rows(0)("flb_type").ToString()
                    txtFlbType.Tag = dtt.Rows(0)("flb_material_code").ToString()
                End If
            Else
                txt_mes_project_code.Text = String.Empty
                txt_mes_project_name.Text = String.Empty
                lblProdSpecName.Text = String.Empty
                txtFlbType.Text = String.Empty
            End If
        End If
    End Sub

    Private Sub btnSet_Click(sender As Object, e As EventArgs) Handles btnSet.Click
        If String.IsNullOrEmpty(cmbOpers.EditValue.ToString()) = False Then
            Dim formPlanMes As New FormPlanMes()
            formPlanMes.usr = cmbOpers.EditValue.ToString()
            formPlanMes.ShowDialog(Me)
        Else
            MsgBox("请选择操作用户！")
        End If
    End Sub

    Private Sub btnSetProject_Click(sender As Object, e As EventArgs) Handles btnSetProject.Click
        If String.IsNullOrEmpty(cmbOpers.EditValue.ToString()) = False Then
            Dim formMes As New FormMes()
            formMes.ShowDialog(Me)
        Else
            MsgBox("请选择操作用户！")
        End If
    End Sub

    Public Sub setDgvData()
        Dim dtProdInfo As DataTable = objProdInfo.getProdInfoListByStatus("0")
        dgv.AutoGenerateColumns = False
        dgv.DataSource = dtProdInfo
    End Sub

    Private Sub txt_tray_no_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txt_tray_no.KeyPress
        If String.IsNullOrEmpty(txt_tray_no.Text.Trim()) = False Then
            If e.KeyChar = vbCr Then
                '查询托盘信息
                Dim dtTp As DataTable = objTpMst.selectAll(txt_tray_no.Text.Trim())
                '托盘是否存在
                If dtTp.Rows.Count = 1 Then
                    If dtTp.Rows(0)("validity") = "1" Then
                        '托盘是否处于闲的状态
                        If dtTp.Rows(0)("status") = "0" Then
                            '托盘是否符合放入尺寸
                            Dim dtTpDetails As DataTable = objTpDetailsMst.getListByTpNo(txt_tray_no.Text.Trim())
                            Dim b_dtTpDetails As Boolean = False
                            For index = 0 To dtTpDetails.Rows.Count - 1
                                If txtFlbType.Text.Contains(dtTpDetails.Rows(index)("type_value")) Then
                                    b_dtTpDetails = True
                                    Exit For
                                End If
                            Next
                            If b_dtTpDetails Then
                                txt_tray_no_msg.Text = "√"
                            Else
                                txt_tray_no_msg.Text = "与当前生产型号不符"
                            End If
                        Else
                            txt_tray_no_msg.Text = "此托盘号状态：忙"
                        End If
                    Else
                        txt_tray_no_msg.Text = "此托盘号已作废"
                    End If
                Else
                    txt_tray_no_msg.Text = "此托盘号不存在"
                End If
            End If
        End If

    End Sub

    Private Sub txt_tray_no_TextChanged(sender As Object, e As EventArgs) Handles txt_tray_no.TextChanged
        txt_tray_no_msg.Text = String.Empty
    End Sub

    Private Sub txt_dl_no_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txt_dl_no.KeyPress
        If String.IsNullOrEmpty(txt_dl_no.Text.Trim) = False Then
            If e.KeyChar = vbCr Then
                txt_dl_no.Text = txt_dl_no.Text.Trim().ToUpper()
                getDlNoFlb()
            End If
        End If
    End Sub

    Private Sub getDlNoFlb()
        flb_info = objFlbInfo.getFlbInfoByDlNo(txt_dl_no.Text.Trim)
        With flb_info
            If .Rows.Count > 0 Then
                s_id = .Rows(0)("sys_id")
                dlno = .Rows(0)("dl_no")
                If .Rows(0)("which_side") = "0" Then
                    txt_which_side.Text = "朝上"
                Else
                    txt_which_side.Text = "朝下"
                End If
                txt_flb_type.Text = .Rows(0)("flb_type")
                If DBNull.Value.Equals(.Rows(0)("material_code")) Then
                    txt_flb_type.Tag = .Rows(0)("flb_material_code")
                Else
                    txt_flb_type.Tag = .Rows(0)("material_code")
                End If
                txt_flb_count.Text = CInt(.Rows(0)("piece_num")) - CInt(.Rows(0)("used_piece_num"))
            Else
                MsgBox("未检索到法兰板吊篮号")
            End If
        End With
    End Sub

    Private Sub btnChangeDirection_Click(sender As Object, e As EventArgs) Handles btnChangeDirection.Click
        If String.IsNullOrEmpty(txt_which_side.Text.Trim()) = False Then
            If txt_which_side.Text.Trim().Equals("朝上") Then
                Dim rtn1 As Integer = objFlbInfo.updateWhichSideBySysId("1", s_id)
                If rtn1 <> -2 Then
                    MsgBox("调整方向成功", vbYes, "提示")
                Else
                    MsgBox("调整方向失败", vbYes, "提示")
                End If
                getDlNoFlb()
            Else
                Dim rtn0 As Integer = objFlbInfo.updateWhichSideBySysId("0", s_id)
                If rtn0 <> -2 Then
                    MsgBox("调整方向成功", vbYes, "提示")
                Else
                    MsgBox("调整方向失败", vbYes, "提示")
                End If
                getDlNoFlb()
            End If
        Else
            MsgBox("请扫描或输入法兰板吊篮号！", vbYes, "提示")
        End If
    End Sub

    Private Sub txt_dl_no_TextChanged(sender As Object, e As EventArgs) Handles txt_dl_no.TextChanged
        txt_flb_type.Text = String.Empty
        txt_flb_count.Text = String.Empty
        txt_which_side.Text = String.Empty
    End Sub

    Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
        If String.IsNullOrEmpty(cmbOpers.EditValue.ToString()) Then
            MsgBox("请选择操作用户！")
            Exit Sub
        End If
        initForm()
        Dim dtPgdMess As DataTable = objPgdMes.getList(cmb_support_model.SelectedValue.ToString())
        Dim dtPIYzzPgdId As DataTable = objProdInfo.getProdInfoListByYzzPgdId(cmb_support_model.SelectedValue.ToString())
        If CInt(dtPIYzzPgdId.Rows(0)("zx_count")) = CInt(dtPgdMess.Rows(0)("oper_count")) Then
            MsgBox("此生产设定已完成计划要求数量，无法继续生产！请切换生产设定！")
            Exit Sub
        End If
        If String.IsNullOrEmpty(txt_tray_no.Text.Trim()) Then Exit Sub
        If String.IsNullOrEmpty(txt_tray_no_msg.Text.Trim()) Then Exit Sub
        If String.IsNullOrEmpty(txt_dl_no.Text.Trim()) Then Exit Sub
        If String.IsNullOrEmpty(txt_flb_type.Text.Trim()) Then Exit Sub
        If String.IsNullOrEmpty(txt_which_side.Text.Trim()) Then Exit Sub
        If txt_tray_no_msg.Text.Equals("√") Then
            If txt_which_side.Text.Equals("朝上") Then
                If txtFlbType.Tag.Trim().Equals(txt_flb_type.Tag.Trim()) Then
                    Dim prod_id As String = Now.ToString("yyyyMMdd") & "-" & objAutoNumber.getNextKeyNo("prod_code", "prodAutoNumber") '生产编号
                    Dim gyhno As Integer = objAutoNumber.getNextKeyNo("gyh_code_" & objAutoNumber.getGYH_Header(), "gyhAutoNumber")
                    Dim steel_seal_no As String = objAutoNumber.getGYH_Header() & String.Format("{0:D6}", gyhno)  '钢印号
                    Dim project_no As Integer = objAutoNumber.getNextProjectNo(txt_mes_project_code.Text.Trim(), txt_mes_project_name.Text.Trim())
                    Dim project_noo As String = String.Format("{0:D4}", project_no) '项目编号

                    Dim pgd_id As String = cmb_support_model.SelectedValue.ToString()
                    Dim support_model As String = cmb_support_model.Text

                    Dim rtn As Integer = objProdInfo.insertProdInfo(prod_id, pgd_id, support_model, txt_tray_no.Text.Trim, s_id, dlno, steel_seal_no, project_noo, cmbOpers.EditValue.ToString())
                    If rtn <> -2 Then
                        objAutoNumber.setNextKeyNo("prod_code", "prodAutoNumber")
                        objAutoNumber.setNextKeyNo("gyh_code_" & objAutoNumber.getGYH_Header(), "gyhAutoNumber")
                        objAutoNumber.setNextProjectValue(txt_mes_project_code.Text.Trim(), txt_mes_project_name.Text.Trim())
                        initForm()
                        txt_tray_no.Text = String.Empty
                        txt_tray_no_msg.Text = String.Empty
                        txt_dl_no.Text = String.Empty
                        txt_flb_type.Text = String.Empty
                        txt_which_side.Text = String.Empty
                        MsgBox("保存成功")
                        setDgvData()
                    Else
                        MsgBox("保存失败")
                    End If
                Else
                    MsgBox("请确保吊篮内法兰板型号与当前生产支座法兰板型号一致")
                End If
            Else
                MsgBox("吊篮信息有误", vbYes, "提示")
            End If
        Else
            MsgBox("托盘信息有误")
        End If
    End Sub

    Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
        Dim rtn As Integer = objWorkshopCurrent.updateDataClear("0", current_section)
        If rtn = -2 Then
            MsgBox(btnClear.Text + "失败")
        Else
            MsgBox(btnClear.Text + "成功")
            initForm()
        End If
    End Sub

    Private Sub btnRefresh_Click(sender As Object, e As EventArgs) Handles btnRefresh.Click
        initForm()
        setDgvData()
    End Sub

    Private Sub btnUpdateFlbInfo_Click(sender As Object, e As EventArgs) Handles btnUpdateFlbInfo.Click
        Dim formFlbInfoList As New FormFlbInfoList()
        formFlbInfoList.ShowDialog()
    End Sub

    Private Sub btnFlbInfoListUnused_Click(sender As Object, e As EventArgs) Handles btnFlbInfoListUnused.Click
        Dim formFlbInfoListUnused As New FormFlbInfoListUnused()
        formFlbInfoListUnused.ShowDialog()
    End Sub

    Private Sub FormMain_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed
        DBacs.Disconnect()
    End Sub

End Class
